<?php

/**
 * DetalleTrabajo filter form.
 *
 * @package    laboratorio
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DetalleTrabajoFormFilter extends BaseDetalleTrabajoFormFilter
{
  public function configure()
  {
    parent::configure();
    
    $q = Doctrine_Query::create()
      ->from('Grupoprod g')
      ->leftJoin('g.Producto p')
      ->where('grupo_id <> 0')
      ->orderBy('g.nombre')
      ->addOrderBy('p.nombre');
     
    $res = $q->fetchArray();
    
    $choices = array("" => "");
    
    foreach($res as $grupos){
      $prods = array();
      foreach($grupos['Producto'] as $prod){
        $prods[$prod['id']] = $prod['nombre'];  
      }
      $choices[$grupos['nombre']] = $prods;
    }  
   
    $this->widgetSchema['cliente_id'] = new sfWidgetFormDoctrineChoice(array('model' => 'Cliente', 'add_empty' => true, 'order_by' => array('apellido', 'asc')), array('data-placeholder' => 'Escriba un Nombre...', 'class' => 'chzn-select', 'style' => 'width:350px;'));    
    $this->widgetSchema['producto_id'] = new sfWidgetFormChoice(array('choices' => $choices), array('data-placeholder' => 'Escriba un Nombre...', 'class' => 'chzn-select', 'style' => 'width:450px;'));    
    
    $this->validatorSchema['cliente_id'] = new sfValidatorInteger(array('required' => false));
    $this->validatorSchema['producto_id'] = new sfValidatorInteger(array('required' => false));
    
    $this->widgetSchema['fecha'] = new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDateJQueryUI(array("change_month" => true, "change_year" => true)), 'to_date' => new sfWidgetFormDateJQueryUI(array("change_month" => true, "change_year" => true)), 'with_empty' => false, 'template' => 'desde %from_date%<br />hasta %to_date%'));
    $this->validatorSchema['fecha'] = new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false, 'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~')), 'to_date' => new sfValidatorDate(array('required' => false, 'date_format' => '~(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})~'))));      
  }
  
  public function getFields()
  {
    $fields = parent::getFields();
    $fields['cliente_id'] = 'custom';
    $fields['fecha'] = 'custom';
    
    return $fields;
  }  
  
  public function addClienteIdColumnQuery($query, $field, $value)
  {
    $text = $value;
    if($text)
      //$query->leftJoin($query->getRootAlias().'.Trabajo t')->andWhere('(t.cliente_id = ?)', array($text));
      $query->andWhere('(t.cliente_id = ?)', $text);
    return $query;
  }  
  
  public function addFechaColumnQuery($query, $field, $value)
  {
    $val1 = $value['from'];
    $val2 = $value['to'];
    if($val1)
      $query->andWhere('(t.fecha_ingreso >= ? and t.fecha_ingreso <= ?)', array($val1, $val2));
    return $query;
  }  
}
